<a href='https://github.com/angular/angular.js/edit/v1.6.x/src/Angular.js?message=docs(angular.toJson)%3A%20describe%20your%20change...#L1295' class='improve-docs btn btn-primary'><i class="glyphicon glyphicon-edit">&nbsp;</i>Improve this Doc</a>



<a href='https://github.com/angular/angular.js/tree/v1.6.6/src/Angular.js#L1295' class='view-source pull-right btn btn-primary'>
  <i class="glyphicon glyphicon-zoom-in">&nbsp;</i>View Source
</a>


<header class="api-profile-header">
  <h1 class="api-profile-header-heading">angular.toJson</h1>
  <ol class="api-profile-header-structure naked-list step-list">
    
    <li>
      - function in module <a href="api/ng">ng</a>
    </li>
  </ol>
</header>





<div class="api-profile-description">
  <p>Serializes input into a JSON-formatted string. Properties with leading $$ characters will be
stripped since angular uses this notation internally.</p>

</div>



<h2 id="known-issues">Known Issues</h2>
<div class="known-issue">
  <p>The Safari browser throws a <code>RangeError</code> instead of returning <code>null</code> when it tries to stringify a <code>Date</code>
object with an invalid date value. The only reliable way to prevent this is to monkeypatch the
<code>Date.prototype.toJSON</code> method as follows:</p>
<pre><code>var _DatetoJSON = Date.prototype.toJSON;
Date.prototype.toJSON = function() {
  try {
    return _DatetoJSON.call(this);
  } catch(e) {
    if (e instanceof RangeError) {
      return null;
    }
    throw e;
  }
};
</code></pre>
<p>See <a href="https://github.com/angular/angular.js/pull/14221">https://github.com/angular/angular.js/pull/14221</a> for more information.</p>

</div>


<div>
  

  

  <h2 id="usage">Usage</h2>
    
      <p><code>angular.toJson(obj, pretty);</code></p>


    

    
<section class="api-section">
  <h3>Arguments</h3>

<table class="variables-matrix input-arguments">
  <thead>
    <tr>
      <th>Param</th>
      <th>Type</th>
      <th>Details</th>
    </tr>
  </thead>
  <tbody>
    
    <tr>
      <td>
        obj
        
        
      </td>
      <td>
        <a href="" class="label type-hint type-hint-object">Object</a><a href="" class="label type-hint type-hint-array">Array</a><a href="" class="label type-hint type-hint-date">Date</a><a href="" class="label type-hint type-hint-string">string</a><a href="" class="label type-hint type-hint-number">number</a><a href="" class="label type-hint type-hint-boolean">boolean</a>
      </td>
      <td>
        <p>Input to be serialized into JSON.</p>

        
      </td>
    </tr>
    
    <tr>
      <td>
        pretty
        
        <div><em>(optional)</em></div>
      </td>
      <td>
        <a href="" class="label type-hint type-hint-boolean">boolean</a><a href="" class="label type-hint type-hint-number">number</a>
      </td>
      <td>
        <p>If set to true, the JSON output will contain newlines and whitespace.
   If set to an integer, the JSON output will contain that many spaces per indentation.</p>

        <p><em>(default: 2)</em></p>
      </td>
    </tr>
    
  </tbody>
</table>

</section>
    
    <h3>Returns</h3>
<table class="variables-matrix return-arguments">
  <tr>
    <td><a href="" class="label type-hint type-hint-string">string</a><a href="" class="label type-hint type-hint-undefined">undefined</a></td>
    <td><p>JSON-ified string representing <code>obj</code>.</p>
</td>
  </tr>
</table>

  
  
  



  
</div>


